package com.leo.erp.service.impl;

import java.util.List;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leo.erp.dao.MsgMapper;
import com.leo.erp.dto.MsgExecution;
import com.leo.erp.entity.Msg;
import com.leo.erp.entity.User;
import com.leo.erp.enums.MsgStateEnum;
import com.leo.erp.service.MsgService;
import com.leo.erp.util.GlobalRuntimeException;

@Service
public class MsgServiceImpl implements MsgService{
	@Autowired
	private MsgMapper msgMapper;

	@Override
	public MsgExecution getMsgCountByStatus(String status) {
		MsgExecution me = new MsgExecution();
		QueryWrapper<Msg> queryWrapper = new QueryWrapper<>();
		try {
			//获取用户信息,session
			HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
	        User user = (User)request.getSession().getAttribute("user");
	        if(status != null && status != "") {
	        	queryWrapper.eq("status", status);
	        	queryWrapper.eq("tenantId", user.getUserId());
	        	queryWrapper.ne("deleteFlag", "1");
	        	int result = msgMapper.selectCount(queryWrapper);
	        	if(result > 0) {
	        		me.setState(MsgStateEnum.SUCCESS.getState());
	        		me.setCount(result);
	        	}else {
	        		me.setState(MsgStateEnum.OFFLINE.getState());
	        		me.setCount(0);
	        	}
	        }
	        
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		
		return me;
	}
}
